﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Data.SqlClient;


public partial class kecheng_manage_jigou : System.Web.UI.Page
{

    string s_where, s_order;
    string curindex = String.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {
        Session["sTittle"] = "管理课程";
        if (!IsPostBack)
        {
            DataBrid();
        }

    }


    // 李红新更改 2010-09-17 绑定数据
    private void DataBrid()
    {
        int record = 0;
        int count = 0;
        s_order = "ORDER BY [id] DESC";
        s_where = string.Format(" where [jigou] = '{0}'", Session["sJigou"].ToString()); ;
        string sqlText = "*";
        DataTable DT = DbHelper.ExecutePage(sqlText, "[kecheng]" + s_where, "id", s_order, Pager.CurrentPageIndex, Pager.PageSize, out record, out count, null);
        Pager.RecordCount = record;
        this.GridView1.DataSource = DT;
        this.GridView1.DataBind();
    }



    //李红新更改 2010-09-17 条件绑定数据
    private void DataBrid(string sqlwhere)
    {
        int record = 0;
        int count = 0;
        s_order = "ORDER BY [kid] DESC";
        //s_order = "ORDER BY [xtsj2] DESC";
        string sqlText = "k.id as kid,k.*,a.username1";
        DataTable DT = DbHelper.ExecutePage(sqlText, "[kecheng]" + sqlwhere, "k.id", s_order, Pager.CurrentPageIndex, Pager.PageSize, out record, out count, null);
        Pager.RecordCount = record;
        this.GridView1.DataSource = DT;
        this.GridView1.DataBind();
    }



    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)    //自动编号功能
    {
        //if (e.Row.RowType == DataControlRowType.DataRow)
        //{
        //    //当鼠标停留时更改背景色
        //    e.Row.Attributes.Add("onmouseover", "c=this.style.backgroundColor;this.style.backgroundColor='#80BA21'");
        //    //当鼠标移开时还原背景色
        //    e.Row.Attributes.Add("onmouseout", "this.style.backgroundColor=c");
        //}
    }

    protected void btnSearch_Click(object sender, EventArgs e)
    {
        string strWhere = " as k left join account as a on k.laoshi = a.username where k.jigou='" + Session["sJigou"] + "'";
        if (!string.IsNullOrEmpty(txtKecheng1.Text))
        {
            strWhere += " and k.name like '%" + txtKecheng1.Text + "%'";
        }
        if (!string.IsNullOrEmpty(typeList.SelectedValue))
        {
            strWhere += " and k.kechenglx='" + typeList.SelectedValue + "'";
        }
        if (!string.IsNullOrEmpty(statusList.SelectedValue))
        {
            strWhere += " and k.confirm='" + statusList.SelectedValue + "'";
        }
        if (!string.IsNullOrEmpty(StartDate.Value))
        {
            strWhere += " and k.xtsj2 >='" + StartDate.Value + "'";
        }
        if (!string.IsNullOrEmpty(EndDate.Value))
        {
            strWhere += " and k.xtsj2 <'" + EndDate.Value + "'";
        }
        if (!string.IsNullOrEmpty(teacher.Text))
        {
            strWhere += " and a.username1 like '%" + teacher.Text + "%'";
        }
        if (strWhere != " as k left join account as a on k.laoshi = a.username where k.jigou='" + Session["sJigou"] + "'")
        {
            Pager.CurrentPageIndex = 0;
            DataBrid(strWhere);
            ViewState["sqlwhere"] = strWhere;
        }
        else
        {
            ViewState["sqlwhere"] = null;
            Pager.CurrentPageIndex = 0;
            DataBrid();
        }
    }


    protected void pager_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
    {
        Pager.CurrentPageIndex = e.NewPageIndex;
        if (ViewState["sqlwhere"] != null)
        {
            DataBrid(ViewState["sqlwhere"].ToString());
        }
        else
        {
            DataBrid();
        }

    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        if (GridView1.Rows.Count > 0)
        {
            string sql = string.Empty;
            for (int i = 0; i < GridView1.Rows.Count; i++)
            {
                if (((CheckBox)GridView1.Rows[i].Cells[0].FindControl("CheckBox1")).Checked)
                {
                    sql += string.Format(" id={0} or ", GridView1.Rows[i].Cells[1].Text);
                }
            }
            if (!String.IsNullOrEmpty(sql))
            {
                sql = sql.Substring(0, sql.Length - 4);
                sql = string.Format("delete from kecheng where {0}", sql);
                int num = DbHelper.ExecuteNonQuery(CommandType.Text, sql, null);
                if (num > 0)
                {
                    ClientScript.RegisterStartupScript(GetType(), "deleteSuccess", string.Format("alert('{0}');", "删除成功"), true);
                    DataBrid();
                }
                else
                {
                    ClientScript.RegisterStartupScript(GetType(), "deleteError", string.Format("alert('{0}');", "删除失败"), true);
                }
            }
        }
    }
}
